const db = require('../db/dbs');
const mysql = require('mysql');

function Flowers(FlowerID) {
    this.FlowerID = FlowerID;
}

// 获取花朵数据的方法
Flowers.prototype.getFlowerData = async function(dataType) {
    const escapedDataType = mysql.escapeId(dataType);
    console.log('查询的数据类型：', dataType, '收到的flowerID:', this.FlowerID);
    const selectQuery = `SELECT ${escapedDataType} FROM flower WHERE FlowerID = ?`;
    try {
        const rows = await db.query(selectQuery, [this.FlowerID]);
        if (rows.length > 0) {
            return rows[0][dataType];
        } else {
            throw new Error(`找不到花朵数据或数据类型 ${dataType}`);
        }
    } catch (error) {
        throw new Error(`无法获取花朵数据: ${error.message}`);
    }
};

module.exports = Flowers;
